﻿namespace HutongGames.PlayMaker
{
    using System;
    using UnityEngine;

    public static class ActionHelpers
    {
        private static float mousePickDistanceUsed;
        public static RaycastHit mousePickInfo;
        private static int mousePickLayerMaskUsed;
        private static float mousePickRaycastTime;
        private static Texture2D whiteTexture;

        public static string CheckForValidEvent(FsmState state, string eventName)
        {
            if (state == null)
            {
                return "Invalid State!";
            }
            if (string.IsNullOrEmpty(eventName))
            {
                return "";
            }
            foreach (FsmTransition transition in state.Fsm.GlobalTransitions)
            {
                if (transition.EventName == eventName)
                {
                    return "";
                }
            }
            foreach (FsmTransition transition2 in state.Transitions)
            {
                if (transition2.EventName == eventName)
                {
                    return "";
                }
            }
            return ("Fsm will not respond to Event: " + eventName);
        }

        public static string CheckOwnerPhysicsSetup(GameObject gameObject)
        {
            string str = string.Empty;
            if (((gameObject != null) && (gameObject.collider == null)) && (gameObject.rigidbody == null))
            {
                str = str + "Owner requires a RigidBody or Collider!\n";
            }
            return str;
        }

        public static string CheckPhysicsSetup(FsmOwnerDefault ownerDefault)
        {
            if (ownerDefault == null)
            {
                return "";
            }
            if (ownerDefault.OwnerOption != OwnerDefaultOption.UseOwner)
            {
                return CheckPhysicsSetup(ownerDefault.GameObject.Value);
            }
            return CheckOwnerPhysicsSetup(ownerDefault.GameObject.Value);
        }

        public static string CheckPhysicsSetup(GameObject gameObject)
        {
            string str = string.Empty;
            if (((gameObject != null) && (gameObject.collider == null)) && (gameObject.rigidbody == null))
            {
                str = str + "GameObject requires RigidBody/Collider!\n";
            }
            return str;
        }

        public static string CheckRayDistance(float rayDistance)
        {
            if (rayDistance > 0f)
            {
                return "";
            }
            return "Ray Distance should be greater than zero!\n";
        }

        public static void DebugLog(Fsm fsm, HutongGames.PlayMaker.LogLevel logLevel, string text)
        {
            if (FsmLog.LoggingEnabled)
            {
                switch (logLevel)
                {
                    case HutongGames.PlayMaker.LogLevel.Info:
                        fsm.MyLog.LogAction(FsmLogType.Info, text);
                        return;

                    case HutongGames.PlayMaker.LogLevel.Warning:
                        fsm.MyLog.LogAction(FsmLogType.Warning, text);
                        return;

                    case HutongGames.PlayMaker.LogLevel.Error:
                        fsm.MyLog.LogAction(FsmLogType.Error, text);
                        return;
                }
            }
        }

        private static void DoMousePick(float distance, int layerMask)
        {
            if (Camera.main != null)
            {
                Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out mousePickInfo, distance, layerMask);
                mousePickLayerMaskUsed = layerMask;
                mousePickDistanceUsed = distance;
                mousePickRaycastTime = Time.frameCount;
            }
        }

        public static PlayMakerFSM GetGameObjectFsm(GameObject go, string fsmName)
        {
            if (!string.IsNullOrEmpty(fsmName))
            {
                foreach (PlayMakerFSM rfsm in go.GetComponents<PlayMakerFSM>())
                {
                    if (rfsm.FsmName == fsmName)
                    {
                        return rfsm;
                    }
                }
            }
            return go.GetComponent<PlayMakerFSM>();
        }

        public static Vector3 GetPosition(FsmGameObject fsmGameObject, FsmVector3 fsmVector3)
        {
            if (fsmGameObject.Value != null)
            {
                return (!fsmVector3.IsNone ? fsmGameObject.Value.transform.TransformPoint(fsmVector3.Value) : fsmGameObject.Value.transform.position);
            }
            return fsmVector3.Value;
        }

        public static int GetRandomWeightedIndex(FsmFloat[] weights)
        {
            float max = 0f;
            foreach (FsmFloat num2 in weights)
            {
                max += num2.Value;
            }
            float num3 = UnityEngine.Random.Range(0f, max);
            for (int i = 0; i < weights.Length; i++)
            {
                if (num3 < weights[i].Value)
                {
                    return i;
                }
                num3 -= weights[i].Value;
            }
            return -1;
        }

        public static bool HasAnimationFinished(AnimationState anim, float prevTime, float currentTime)
        {
            if ((anim.wrapMode == WrapMode.Loop) || (anim.wrapMode == WrapMode.PingPong))
            {
                return false;
            }
            return ((((anim.wrapMode == WrapMode.Default) || (anim.wrapMode == WrapMode.Once)) && ((prevTime > 0f) && currentTime.Equals((float) 0f))) || ((prevTime < anim.length) && (currentTime >= anim.length)));
        }

        public static bool IsLoopingWrapMode(WrapMode wrapMode)
        {
            if (wrapMode != WrapMode.Loop)
            {
                return (wrapMode == WrapMode.PingPong);
            }
            return true;
        }

        public static bool IsMouseOver(GameObject gameObject, float distance, int layerMask)
        {
            if (gameObject == null)
            {
                return false;
            }
            return (gameObject == MouseOver(distance, layerMask));
        }

        public static bool IsVisible(GameObject go)
        {
            return (((go != null) && (go.renderer != null)) && go.renderer.isVisible);
        }

        public static int LayerArrayToLayerMask(FsmInt[] layers, bool invert)
        {
            int num = 0;
            foreach (FsmInt num2 in layers)
            {
                num |= ((int) 1) << num2.Value;
            }
            if (invert)
            {
                num = ~num;
            }
            if (num != 0)
            {
                return num;
            }
            return -5;
        }

        public static GameObject MouseOver(float distance, int layerMask)
        {
            if ((!mousePickRaycastTime.Equals((float) Time.frameCount) || (mousePickDistanceUsed < distance)) || (mousePickLayerMaskUsed != layerMask))
            {
                DoMousePick(distance, layerMask);
            }
            if ((mousePickInfo.collider != null) && (mousePickInfo.distance < distance))
            {
                return mousePickInfo.collider.gameObject;
            }
            return null;
        }

        public static RaycastHit MousePick(float distance, int layerMask)
        {
            if ((!mousePickRaycastTime.Equals((float) Time.frameCount) || (mousePickDistanceUsed < distance)) || (mousePickLayerMaskUsed != layerMask))
            {
                DoMousePick(distance, layerMask);
            }
            return mousePickInfo;
        }

        [Obsolete("Use LogError instead.")]
        public static void RuntimeError(FsmStateAction action, string error)
        {
            action.LogError(action + " : " + error);
        }

        public static Texture2D WhiteTexture
        {
            get
            {
                if (whiteTexture == null)
                {
                    whiteTexture = new Texture2D(1, 1);
                    whiteTexture.SetPixel(0, 0, Color.white);
                    whiteTexture.Apply();
                }
                return whiteTexture;
            }
        }
    }
}

